<?php
namespace app\index\controller;

use app\common\model\Category;
use categoryclass\Categoryclass;
use think\Cache;
use think\Db;
use think\Config;
use think\Controller;

class CommonController extends Controller
{
    //初始化操作
    protected function _initialize()
    {
        parent::_initialize();
        //1.导航栏
        //$category_list = Db::name('category')->where('parent_id',0)->order('sort_order desc')->select();
        // 新增缓存功能
        $category_list = Cache::remember('category_list', function (){
            $cate = Category::order('sort_order desc')->select();
            return Categoryclass::orm_catesorts($cate);
        });

        $this->assign('category_list', $category_list);

        //2.热门博文 6篇
        $hots = Cache::remember('hots', function (){
            return Db::name('article')->field('id,title')->order('view desc')->limit(6)->select();
        });
        $this->assign('hots', $hots);

        //3.推荐文章 6
        $remmends = Cache::remember('remmends', function () {
           return Db::name('article')->field('id,title')->order('art_order desc')->limit(6)->select();
        });
        $this->assign('remmends', $remmends);
        //4.Tags

        //5.友情链接
        $links = Cache::remember('links', function () {
           return Db::name('friend')->field('id,name,link')->where('del', 0)->order('sort_order desc')->limit(6)->select();
        });

        $this->assign('links', $links);
        //6.查询关键词等配置信息
        $config = Config::get('web');
        //dump($config);
        $this->assign('config', $config);
    }
}
